package com.neusoft.yuejian12.yuejian_12.activity;

import android.app.Activity;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.neusoft.yuejian12.yuejian_12.R;
import com.neusoft.yuejian12.yuejian_12.adapter.ClubListAdapter;
import com.neusoft.yuejian12.yuejian_12.fragment.ClubFragment;
import com.neusoft.yuejian12.yuejian_12.javabean.ClubBean;
import com.neusoft.yuejian12.yuejian_12.model.ClubModel;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SearchClubActivity extends Activity {
    private EditText search_club;
    private ImageView searchClub_button;
    private RecyclerView recyclerView;
    private String t_name;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_club);
        initView();
        initEvent();

    }

    private void initView() {
        search_club = (EditText) findViewById(R.id.search_club);
        searchClub_button = (ImageView) findViewById(R.id.searchClub_button);
        recyclerView = (RecyclerView) findViewById(R.id.searchClubRV);
        //recyclerView.setLayoutManager(new LinearLayoutManager(this));//设置布局
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        SpacesItemDecoration decoration=new SpacesItemDecoration(6);
        recyclerView.addItemDecoration(decoration);

    }
    public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
        private int space;
        public SpacesItemDecoration(int space) {
            this.space=space;
        }
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.left=space;
            outRect.right=space;
            outRect.bottom=space;
            if(parent.getChildAdapterPosition(view)==0){
                outRect.top=space;
            }
        }
    }
    private void initEvent() {
        searchClub_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String c_name = search_club.getText().toString();
                //  Log.d("A", "t_name");
                getData(c_name);
            }
            private void getData(String c_name) {
             ClubModel clubModel = new ClubModel();
                Call<List<ClubBean>> call = clubModel.searchClub(c_name);
                //System.out.print("t_name");
                call.enqueue(new Callback<List<ClubBean>>() {
                    @Override
                    public void onResponse(Call<List<ClubBean>> call, Response<List<ClubBean>> response) {
                        List<ClubBean> data = response.body();  //获取数据
                        // System.out.printf("data");
                        recyclerView.setAdapter(new ClubListAdapter(SearchClubActivity.this, data));
                    }

                    @Override
                    public void onFailure(Call<List<ClubBean>> call, Throwable t) {

                    }
                });
            }
        });
    }

}